package com.itcast.plane.modoule.path.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.plane.common.ConsHint;
import com.itcast.plane.common.PageIndex;
import com.itcast.plane.modoule.flight.domain.Flight;
import com.itcast.plane.modoule.flight.mapper.FlightMapper;
import com.itcast.plane.modoule.path.domain.Path;
import com.itcast.plane.modoule.path.dto.PathCreatDTO;
import com.itcast.plane.modoule.path.mapper.PathMapper;
import com.itcast.plane.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PathService extends ServiceImpl<PathMapper, Path> {

    @Resource
    private PathMapper pathMapper;

    @Resource
    private FlightMapper flightMapper;

    public JsonResult addPath(PathCreatDTO creatDTO) {
        Path path = new Path();
        BeanUtils.copyProperties(creatDTO, path);
        int count = count(path);

        if (count == 0) {
            int line = pathMapper.insert(path);
            if (line > 0) {
                return new JsonResult(true, ConsHint.INSERT_SUCCESS);
            } else {
                return new JsonResult(false, ConsHint.INSERT_DEFAULT);
            }
        } else {
            return new JsonResult(false, ConsHint.INSERT_DEFAULTED);
        }

    }

    public JsonResult deletePath(int id) {
        LambdaQueryWrapper<Flight> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        int i = flightMapper.selectCount(lambdaQueryWrapper.eq(Flight::getPathId, id));
        if (i > 0) {
            return new JsonResult(false, ConsHint.DELETE_DEFAULT_USING);
        }
        pathMapper.deleteById(id);
        return new JsonResult(true, ConsHint.DELETE_SUCCESS);
    }

    public JsonResult editPath(Path path) {
        int count = count(path);
        if (count == 0) {
            pathMapper.updateById(path);
            return new JsonResult(true, ConsHint.EDIT_SUCCESS);
        } else {
            return new JsonResult(false, ConsHint.EDIT_DEFAULT);
        }
    }

    public JsonResult getPage(int pages) {
        LambdaQueryWrapper<Path> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Path::getId).eq(Path::getIfDelete, 0);
        IPage<Path> page = page(new Page<>(pages, PageIndex.pageSize), lambdaQueryWrapper);
        return new JsonResult(true, page);
    }

    public JsonResult getAll() {
        LambdaQueryWrapper<Path> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Path> list = pathMapper.selectList(lambdaQueryWrapper.eq(Path::getIfDelete, 0));
        return new JsonResult(true, list);
    }

    private int count(Path path) {
        LambdaQueryWrapper<Path> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Path> list = pathMapper.selectList(lambdaQueryWrapper.eq(Path::getDeparture, path.getDeparture()));
        int count = 0;
        for (Path path1 : list) {
            if (path1.getDestination().equals(path.getDestination())) {
                count++;
            }
        }
        return count;
    }
}
